package com.atguigu.dga.governance.assessor.calc;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

@Component("NO_ACCESS")
public class NoAccessAssessor extends Assessor {


    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail)  {
       //获得指标参数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        Integer days = paramJsonObj.getInteger("days");

        //判断的逻辑 ：  最后访问日期 + days  小于 考评日期   给0分
        // 最后访问日期
        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();
        // +days

        Date tableLastAccessTimeForAssess = DateUtils.addDays(tableLastAccessTime, days);
        //考评日期
        String assessDate = assessParam.getAssessDate();
        Date assessDateDate=null;
        try {
              assessDateDate = DateUtils.parseDate(assessDate, "yyyy-MM-dd");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        // 0 一样大  1 前大后小  -1 前小后大
        if(tableLastAccessTimeForAssess.compareTo(assessDateDate)<0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期未被访问，最后访问日期："+tableLastAccessTime);
        }

    }
}
